<?php /* УТФ-8:fix */

class SPImodel__songnvideo__manage extends SPImodel__songnvideo
{

	public function select_by_status($status){
		
		$store = SPIstore::I();
		
        $query = " SELECT * "
               . " FROM ".SPIcfg::$store['table:content']
               . " WHERE type = 'song' AND "
			   . $store->prepare_search_query( 'workstatus', $status ) 
               . " ORDER BY order_index ;"  ;
		return $store->query( $query );
	}
	
	public function getNextOrderIndex ($status)
	{
		$store = SPIstore::I();
		
        $query = " SELECT order_index "
               . " FROM ".SPIcfg::$store['table:content']
               . " WHERE type = 'song' AND "
			   . $store->prepare_search_query( 'workstatus', $status ) 
               . "order by order_index DESC;"  ;
		
		if($result = $store->query( $query )->fetch_and_close()){
			$next = $result['order_index'] + 1;
		}
		else{
			$next = 1;
		}
		return $next;
		
	}

    public function select( $type, $filter_search = '', $orderby = null )
    {
        switch( $orderby['dir'] )
        {
            default:
            case '+':           $orderby['dir'] = 'ASC';  break;
            case '-':           $orderby['dir'] = 'DESC'; break;
        }

        switch( $orderby['field'] )
        {
            default:
            case 'name':        $q_orderby = "name {$orderby['dir']}, anons {$orderby['dir']}, id {$orderby['dir']}"; break;
            case 'order_index': $q_orderby = "order_index {$orderby['dir']}, id DESC"; break;
            case 'id':          $q_orderby = "id {$orderby['dir']}"; break;
        }

        // --------------------------------------------------------------------

        switch( $type )
        {
            case 'song':
            case 'video':           $q_type = " ( type = '{$type}' ) "; break;

            default:                $q_type = " FALSE "; break;
        }

        // --------------------------------------------------------------------

        $store = SPIstore::I();

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE TRUE "
               . ( $q_type ? " AND {$q_type} " : '' )
               . ( $filter_search
                 ? '     AND (  ' . $store->prepare_search_query( 'id', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'name', $filter_search )
                 . '         OR ' . $store->prepare_search_query( 'anons', $filter_search )
                 . '         ) '
                 : ''
                 )
               . " ORDER BY workstatus, {$q_orderby} "
               ;

        return $store->query( $query );
    }

    public function select_for_sort( $type, $sort_ord )
    {
        switch( $type )
        {
            case 'song':
            case 'video':           $q_type = " ( type = '{$type}' ) "; break;

            default:                $q_type = " FALSE "; break;
        }
		
		if($sort_ord=='top')
			$workst=1;
		else
			$workst=2;

        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE workstatus=".$workst." "
               . ( $q_type ? " AND {$q_type} " : '' )
               . " ORDER BY order_index ASC, id DESC "
               ;

        return SPIstore::I()->query( $query );
    }
	
    public function get_songs_in_top()
    {
        $query =    " SELECT COUNT(*) AS count "
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE workstatus=1 AND type='song' "
               ;

        $item = SPIstore::I()->query( $query )->fetch_and_close();

        return $item['count'];
    }

    public function get_by_id( $item_id )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:content']
               .     " WHERE id=" . (int) $item_id
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function set_activity( $set_activity_value, $item_id )
    {
		switch($set_activity_value){
			case 'on_top':
				return SPIstore::I()->modify(
					SPIcfg::$store['table:content'],
					array( 'workstatus' => 1, 'order_index'=>-1 ),
					array( 'id' => $item_id )
				);

			case 'on_new':
				return SPIstore::I()->modify(
					SPIcfg::$store['table:content'],
					array( 'workstatus' => 2, 'order_index'=>-1 ),
					array( 'id' => $item_id )
				);

			case 'off':
				return SPIstore::I()->modify(
					SPIcfg::$store['table:content'],
					array( 'workstatus' => 0, 'order_index'=>-1 ),
					array( 'id' => $item_id )
				);
		}
    }

    public function insert( $fields )
    {
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id();

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:content'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_id, $fields )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:content'],
            $fields,
            array( 'id' => $item_id )
        );
    }

    public function reset_order_indexes( $type, $sort_on )
    {
		switch($sort_on){
			case 'top':
				$workstatus=1;
			break;
			case 'new':
				$workstatus=2;
			break;
			default:
				$workstatus=0;
			break;
		}
		
        return SPIstore::I()->modify(
            SPIcfg::$store['table:content'],
            array( 'order_index'=>-1 ),
            array( 'type' => $type, 'workstatus' => $workstatus, 'workstatus' => 0 )
        );
    }

    public function delete( $item_id )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:content'],
            array( 'id' => $item_id )
        );
    }
}
